<?php

namespace app\api\controller;

use app\api\model\ConfigPrice;
use app\api\model\Juhe;
use app\api\model\OrderCommon;
use app\api\model\Topup;
use OSS\Core\OssException;
use OSS\OssClient;
use think\Db;
use app\api\model\Cluster;
use think\Request;
use think\Log;
use app\api\model\ConfigSystem as Config;
use app\api\model\User;
use app\api\model\Dymanic;
use app\api\model\Timing as TimingModel;
use app\api\model\UserBalance;
use app\api\model\UserData;
use app\api\model\UserTeam;
use app\api\model\UserActiveval;
use app\api\model\YaoqingRecords;
use app\api\model\Privatechat;
use app\api\model\UserPunish;
use app\api\model\VolleyDesc as Balance;
use app\api\model\PrivatechatDeleTask;
use app\api\model\UserPornoStatis;
use app\service\UserIntimate;
use GatewayClient\Gateway;
use Mrgoon\AliSms\AliSms;
use Qcloud\Cos\Client;
use app\service\UserRepeat;
use think\Cache;
use app\service\BaiduApi;

//向新的的汇付账户创建用户及结算账户
class Adapayuser extends Common
{
    /**
     * 向新的的汇付账户创建用户及结算账户
     */
    public function createuser()
    {
        return '线上不允许执行';
        set_time_limit(0);//尽量本地数据库去跑 不要放到线上跑
        // 查询user表中符合条件的人员账户
        // 执行查询
        $where="reg=1 and delete_time=0 and register_datetime>='2025-05-08 00:00:00' and phone!='' and phone is not null and id!=6382";
        $result = Db::name('user')
            ->field("id, name,phone, register_datetime, bank_user_name, bank, bank_number")
            ->where($where)
            ->order('id', 'asc')
            //->limit(1)
            ->select();
        //echo Db::name('user')->getLastSql();
        if (empty($result)) {
            return '未取到有效数据'; // 无数据直接返回
        }
        foreach($result as $k=>$v){
            $member_id=$v['id'];
            $res = action('api/adapay/create_user', $member_id);
            //print_r($res->getData());die;
            if($res->getData()['code'] != 200){//记录用户创建失败的日志
                $ucerr=array(
                    'uid'=>$member_id,
                    'fh'=>$res->getData()
                );
                file_put_contents(
                    "extend/log/usercreate_err.txt",
                    json_encode($ucerr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL,
                    FILE_APPEND | LOCK_EX
                );
            }
                //创建用户结算账户
                //首先查询用户身份证信息
                $cert_no = Db::name('user_certification')->where(['types'=>1,'user_id'=>$member_id, 'status'=>1])->value('data');
                if(!$cert_no){//未找到身份证则跳过此次创建
                    continue;
                }
                $account_info=[
                    'card_name' => $v['bank_user_name'],            //认证持卡人
                    'tel_no' => $v['phone'],                        //银行预留手机号
                    'cert_id' => $cert_no,                          //身份证号
                    'card_id' => $v['bank_number'],                 //银行卡号
                    'bank_name' => $v['bank'],                      //开户行名称
                    'cert_type' => '00',
                    'bank_acct_type' => 2,
                ];
                $res1 = action('api/adapay/set_settle_account', ['member_id'=>$member_id, 'account_info'=>$account_info]);
                if($res1->getData()['code'] != 200){//记录结算账户创建失败日志
                    $uacerr=array(
                        'uid'=>$member_id,
                        'payload'=>$account_info,
                        'fh'=>$res1->getData()
                    );
                    file_put_contents(
                        "extend/log/usercreatea_err.txt",
                        json_encode($uacerr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL,
                        FILE_APPEND | LOCK_EX
                    );
                }
            usleep(500000); // 休眠500毫秒
        }
    }
}